//
//  ModuleViewController.m
//  Game2048
//
//  Created by tarena on 16/3/24.
//  Copyright © 2016年 tarena. All rights reserved.
//

#import "ModuleViewController.h"
#import "ModuleView.h"
#import "ModuleModel.h"

@interface ModuleViewController ()<ModuleDelegate>
{
    UInt16 (*moduleData)[4];
   
}

@property (nonatomic ,strong) ModuleView *moduleView;

@property (nonatomic ,strong) ModuleModel *moduleModel;

@property (nonatomic ,strong) UISwipeGestureRecognizer *swipUp;
@property (nonatomic ,strong) UISwipeGestureRecognizer *swipDown;
@property (nonatomic ,strong) UISwipeGestureRecognizer *swipLeft;
@property (nonatomic ,strong) UISwipeGestureRecognizer *swipRight;

@end

@implementation ModuleViewController

-( UISwipeGestureRecognizer *)swipUp{
    if (!_swipUp) {
        _swipUp = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipAction:)];
        _swipUp.direction = UISwipeGestureRecognizerDirectionUp;
    }
    return _swipUp;
}

-( UISwipeGestureRecognizer *)swipDown{
    if (!_swipDown) {
        _swipDown = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipAction:)];
        _swipDown.direction = UISwipeGestureRecognizerDirectionDown;
    }
    return _swipDown;
}


-( UISwipeGestureRecognizer *)swipLeft{
    if (!_swipLeft) {
        _swipLeft = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipAction:)];
        _swipLeft.direction = UISwipeGestureRecognizerDirectionLeft;
    }
    return _swipLeft;
}

-( UISwipeGestureRecognizer *)swipRight{
    if (!_swipRight) {
        _swipRight = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipAction:)];
        _swipRight.direction = UISwipeGestureRecognizerDirectionRight;
    }
    return _swipRight;
}

-( ModuleView *)moduleView{
    if (!_moduleView) {
        
        _moduleView = [[ModuleView alloc]initWithFrame:CGRectMake(0, 60 + (self.view.bounds.size.height - self.view.bounds.size.width - 160)/2, self.view.bounds.size.width, self.view.bounds.size.width)];
        _moduleView.backgroundColor = [UIColor colorWithWhite:0.80 alpha:1.0];
        _moduleView.moduleData = moduleData;
        [_moduleView addGestureRecognizer:self.swipUp];
        [_moduleView addGestureRecognizer:self.swipDown];
        [_moduleView addGestureRecognizer:self.swipLeft];
        [_moduleView addGestureRecognizer:self.swipRight];
        
    }
    return _moduleView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.moduleModel = [ModuleModel new];
    self.moduleModel.delegate = self;
    moduleData = [self.moduleModel loadModuleData];
    [self.view addSubview:self.moduleView];
    
    
}


- (void)refreshBtnClick:(id)sender{
    
    bzero(moduleData, sizeof(moduleData));
    [self.moduleView setNeedsDisplay];
    
}


- (void)moduleMoveDestRow:(UInt8)destRow
                  DesrCol:(UInt8)destCol
                   SrcRow:(UInt8)srcRow
                   SrcCol:(UInt8)srcCol{
    UILabel *destModule = self.moduleView.moduleArray[destRow][destCol];
    UILabel *srcModule = self.moduleView.moduleArray[srcRow][srcCol];
    
    CGRect destFrame = destModule.frame;
    CGRect srcFrame = srcModule.frame;
    [UIView animateWithDuration:0.3 animations:^{
        srcModule.frame = destFrame;
    }];
    destModule.frame = srcFrame;
    self.moduleView.moduleArray[destRow][destCol] = srcModule;
    self.moduleView.moduleArray[srcRow][srcCol] = destModule;
    
}


- (void)moduleZoomRow:(UInt8)row Col:(UInt8)col{
    UILabel* module = self.moduleView.moduleArray[row][col];
    module.transform = CGAffineTransformMakeScale(0.5, 0.5);
    module.backgroundColor = [self.moduleView getModuleColor:2];
    module.text = @"2";
    [UIView animateWithDuration:0.2 animations:^()
     {
         module.transform = CGAffineTransformMakeScale(1.0, 1.0);
     }];
    
}
- (void)swipAction:(UISwipeGestureRecognizer *)sender{
    
    switch (sender.direction) {
        case UISwipeGestureRecognizerDirectionUp:
            [self.moduleModel moduleCreate];
            [self.moduleModel moduleUp];
            [self.moduleView setNeedsDisplay];
            NSLog(@"up");
            break;
        case UISwipeGestureRecognizerDirectionDown:
            
            NSLog(@"down");
            break;
        case UISwipeGestureRecognizerDirectionLeft:
            NSLog(@"left");
            break;
        case UISwipeGestureRecognizerDirectionRight:
            NSLog(@"rifht");
            break;
    }
}


@end
